package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.ZslOrigin;
import com.cz.zslu.entity.ZslStoreCommodityType;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.IZslOriginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 品牌管理 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/sys/origin")
@Api(description = "品牌管理")
public class OriginController extends BaseController<IZslOriginService, ZslOrigin> {

    @IgnoreAuth
    @ApiOperation("获取全部品牌分页")
    @RequestMapping(value = "/getAllNewPage", method = RequestMethod.POST)
    public ReturnObject<List<ZslOrigin>> getAllNewPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                       @ApiParam("页数") @RequestParam(defaultValue = "1") int offset) {

        EntityWrapper<ZslOrigin> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.orderBy("crt_time", false);

        Page<ZslOrigin> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @IgnoreAuth
    @ApiOperation("获取全部品牌分页")
    @RequestMapping(value = "/getAllNewPageToBackGround", method = RequestMethod.POST)
    public ReturnObject<List<ZslOrigin>> getAllNewPageToBackGround(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                       @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                       @ApiParam("品牌名称") @RequestParam  String name) {

        EntityWrapper<ZslOrigin> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.orderBy("crt_time", false);
        if (StringUtils.isNotBlank(name)) {
            ew.like("name", name);
        }
        Page<ZslOrigin> obj = baseService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @IgnoreAuth
    @ApiOperation("获取详情")
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<ZslOrigin> findInfoById(@ApiParam("id") @RequestParam Long id) {

        return new ReturnObject<>(0, "获取成功", baseService.selectById(id));
    }

    @ApiOperation("新增")
    @RequestMapping(value = "/saveOriginInfo", method = RequestMethod.POST)
    public ReturnObject<ZslOrigin> saveOriginInfo(@ApiParam("图片url") @RequestParam String picUrl,
                                                  @ApiParam("名称") @RequestParam String name,
                                                  @ApiParam("内容") @RequestParam String content) {

        ZslOrigin info = new ZslOrigin();
        info.setPicUrl(picUrl);
        info.setName(name);
        info.setContent(content);
        baseService.insert(info);

        return new ReturnObject<>(0, "新增成功", null);
    }

    @ApiOperation("编辑")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ReturnObject<ZslOrigin> edit(@ApiParam("id") @RequestParam Long id,
                                        @ApiParam("图片url") @RequestParam String picUrl,
                                        @ApiParam("名称") @RequestParam String name,
                                        @ApiParam("内容") @RequestParam String content) {

        ZslOrigin info = baseService.selectById(id);
        info.setPicUrl(picUrl);
        info.setName(name);
        info.setContent(content);
        baseService.updateById(info);
        return new ReturnObject<>(0, "新增成功", null);
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delOriginById", method = RequestMethod.POST)
    public ReturnObject<ZslOrigin> delOriginById(@ApiParam("id") @RequestParam Long id) {

        ZslOrigin info = baseService.selectById(id);
        info.setState(1);
        baseService.updateById(info);

        return new ReturnObject<>(0, "删除成功", null);
    }

    @IgnoreAuth
    @ApiOperation("获取所有品牌")
    @RequestMapping(value = "/getOriginList", method = RequestMethod.POST)
    public ReturnObject<List<ZslOrigin>> getOriginList() {
        EntityWrapper<ZslOrigin> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        ew.orderBy("crt_time", true);
        return new ReturnObject<>(0, "获取成功", baseService.selectList(ew));
    }

}

